package com.duruikang.LeaveAPI.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.duruikang.LeaveAPI.common.sercurity.utils.JwtTokenUtil;
import com.duruikang.LeaveAPI.dao.TokenDao;
import com.duruikang.LeaveAPI.entity.Token;
import com.duruikang.LeaveAPI.service.SecurityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Drik
 * @date 2021-12-20 17:10
 */
@Service
public class SecurityServiceImpl extends ServiceImpl<TokenDao, Token> implements SecurityService {

    @Autowired
    private JwtTokenUtil jwt;

    @Override
    public Boolean isTokenExist(String token) {
        return query().eq("token", token).count() != 0;
    }

    @Override
    public int registerToken(String token) {
        return baseMapper.insertToken(token);
    }

    @Override
    public int removeToken(String token) {
        return baseMapper.deleteToken(token);
    }

    @Override
    public String checkToken(String token) {
        if(!isTokenExist(token)){
            return"已登出";
        }else {
            if(jwt.isExpiration(token)){
                removeToken(token);
                return "已超时";
            }
        }
        return null;
    }
}
